public class HashBucket2<E,V>{
    //使用泛型
    //通过数组和链表来实现
    public class Node<E,V>{
        public E key;
        public V value;
        public Node<E,V> next;

        public Node(E key,V value){
            this.key=key;
            this.value=value;
        }
    }
    public Node<E,V>[]arry=(Node<E,V>[])new Node[10];
    public int size;
    public void put(E key,V value){
        //找到位置
        int hashCode=key.hashCode();
        int index=hashCode%arry.length;
        //判断当前数组下标的链表有没有key相同，相同则更新value，不同则插入
        Node cur=arry[index];
        while (cur!=null){
            if(cur.key.equals(key)){
                cur.value=value;
            }else{
                cur=cur.next;
            }
        }
        //走到这进行插入(使用头插法)
        Node cut=new Node(key,value);
        cut.next=arry[index];
        arry[index]=cut;
        size++;
        double fuzai=size*1.0f/arry.length;//代表负载因子
        if(fuzai>0.75){
            //扩容,不能直接扩
            Knewarry();
        }
    }
    public void Knewarry(){
        Node[]newarry=new Node[arry.length*2];
        for (int i = 0; i <arry.length ; i++){
            Node cur=arry[i];
            while (cur!=null){
                //插入新数组新链表
                int index=hashCode()% newarry.length;
                Node curN=cur.next;//防止cur.next丢失
                cur.next=newarry[index];
                newarry[index]=cur;
                cur=curN;
            }
        }
        arry=newarry;
    }
    public Object get(E key) {
        int hashCode=key.hashCode();
        int index=hashCode%arry.length;
        Node cur=arry[index];
        while (cur!=null){
            if(cur.key.equals(key)){
                return  cur.value;
            }
            cur=cur.next;
        }
        return null;
    }
    public boolean pek(E key,V value){
        int hashCode=key.hashCode();
        int index=hashCode%arry.length;
        Node cur=arry[index];
        while (cur!=null){
            if(cur.key.equals(key)){
                if(cur.value.equals(value)){
                    return true;
                }
            }
            cur=cur.next;
        }
        return false;
    }
}
